package frontend.components.base;

import frontend.components.other.UserInputCallableAction;
import org.apache.wicket.ajax.AjaxRequestTarget;
import org.apache.wicket.ajax.form.AjaxFormComponentUpdatingBehavior;
import org.apache.wicket.markup.html.basic.Label;
import org.apache.wicket.markup.html.form.DropDownChoice;
import org.apache.wicket.markup.html.form.IChoiceRenderer;
import org.apache.wicket.markup.html.panel.Panel;
import org.apache.wicket.model.LoadableDetachableModel;
import org.apache.wicket.model.PropertyModel;
import org.apache.wicket.model.ResourceModel;

import java.util.List;

public class DoubleDropDownComponent<ModelType, EntityType1, EntityType2> extends Panel {

    public DoubleDropDownComponent(String componentId, String labelResourceKey, String propertyId1, String propertyId2, ModelType uiModel,
                             final List<EntityType1> availableOptions1, final List<EntityType2> availableOptions2,
                             IChoiceRenderer<EntityType1> choiceRender1, IChoiceRenderer<EntityType2> choiceRender2,
                             boolean isNullValid,
                             final UserInputCallableAction<EntityType1> onSelectAction1, final UserInputCallableAction<EntityType2> onSelectAction2) {
        super(componentId);
        Label label = new Label("label", new ResourceModel(labelResourceKey));
        final DropDownChoice<EntityType1> dropDown1 = createDropDown("dropDown1", propertyId1, uiModel, availableOptions1, choiceRender1, isNullValid, onSelectAction1);
        final DropDownChoice<EntityType2> dropDown2 = createDropDown("dropDown2", propertyId2, uiModel, availableOptions2, choiceRender2, isNullValid, onSelectAction2);

        add(label, dropDown1, dropDown2);
        setOutputMarkupId(true);
    }

    private <T> DropDownChoice<T> createDropDown(String componentId, String propertyId, ModelType uiModel, final List<T> availableOptions,
                                                 IChoiceRenderer<T> choiceRender, boolean isNullValid, final UserInputCallableAction<T> onSelectAction) {
        final DropDownChoice<T> dropDown = new DropDownChoice<T>(componentId,
                new PropertyModel<T>(uiModel, propertyId),
                new LoadableDetachableModel<List<T>>() {
                    @Override
                    protected List<T> load() {
                        return availableOptions;
                    }
                },
                choiceRender);
        dropDown.setNullValid(isNullValid);
        if (onSelectAction != null) {
            dropDown.add(new AjaxFormComponentUpdatingBehavior("onchange") {
                @Override
                protected void onUpdate(AjaxRequestTarget target) {
                    T selectedValue = dropDown.getModelObject();
                    onSelectAction.call(target, selectedValue);
                }
            });
        }
        if (!isNullValid && availableOptions.size() == 1) {
            dropDown.setDefaultModelObject(availableOptions.get(0));
        }
        return dropDown;
    }
}
